main.c

					
/*
 * Created: 30.01.2022
 *  Author: Bohdan
 */ 

#include "PIN_avr.h"

/* Setting working mode of a pin (input/output)
 * The function might be not optimal. I couldn't find how to pass a port to a function properly
 * Pins in INPUT mode are configured as WITH Pull UP. Write 0 to PORTx registers to set them to third state mode 
 */
void pin_mode(pin_t Pin, pin_mode_t mode)
{
	switch(Pin.port)
	{
		case _PA:
		{
			switch(mode)
			{
				case INPUT:
				{
				 
				} break; // case INPUT
				case OUTPUT:
				{
					
				} break; // case OUTPUT
			} // switch(mode)
		} break; // case _PA
		case _PB:
		{
			switch(mode)
			{
				case INPUT:
				{
					DDRB &= ~(1<<Pin.pin); // Writing "0" to a pin of DDRB. INPUT
					PORTB |= (1<<Pin.pin); // Writing "1" to a pin of PORTB. Pull UP
				} break; // case INPUT
				case OUTPUT:
				{
					DDRB |= (1<<Pin.pin); // Writing "1" to a pin of DDRB. OUTPUT
				} break; // case OUTPUT
			} // switch(mode)
		} break; // case _PB
		case _PC:
		{
			switch(mode)
			{
				case INPUT:
				{
					DDRC &= ~(1<<Pin.pin);
					PORTC |= (1<<Pin.pin); // Writing "1" to a pin of PORTB. Pull UP
				} break; // case INPUT
				case OUTPUT:
				{
					DDRC |= (1<<Pin.pin);
				} break; // case OUTPUT
			} // switch(mode)
		} break; // case _PC
		case _PD:
		{
			switch(mode)
			{
				case INPUT:
				{
					DDRD &= ~(1<<Pin.pin);
					PORTD |= (1<<Pin.pin); // Writing "1" to a pin of PORTB. Pull UP
				} break; // case INPUT
				case OUTPUT:
				{
					DDRD |= (1<<Pin.pin);
				} break; // case OUTPUT
			} // switch(mode)
		} break; // case _PD
	} // switch(Pin.port)
}

/* Writing OUTPUTS
 */
void pin_write(pin_t Pin, my_bool state)
{
	switch(Pin.port)
	{
		case _PA:
		{
			switch(state)
			{
				case LOW:
				{
					
				} break; // case INPUT
				case HIGH:
				{
					
				} break; // case OUTPUT
			} // switch(mode)
		} break; // case _PA
		case _PB:
		{
			switch(state)
			{
				case LOW:
				{
					PORTB &= ~(1<<Pin.pin); // Writing "0" to a pin of DDRB. INPUT
				} break; // case INPUT
				case HIGH:
				{
					PORTB |= (1<<Pin.pin); // Writing "1" to a pin of DDRB. OUTPUT
				} break; // case OUTPUT
			} // switch(mode)
		} break; // case _PB
		case _PC:
		{
			switch(state)
			{
				case LOW:
				{
					PORTC &= ~(1<<Pin.pin);
				} break; // case INPUT
				case HIGH:
				{
					PORTC |= (1<<Pin.pin);
				} break; // case OUTPUT
			} // switch(mode)
		} break; // case _PC
		case _PD:
		{
			switch(state)
			{
				case LOW:
				{
					PORTD &= ~(1<<Pin.pin);
				} break; // case INPUT
				case HIGH:
				{
					PORTD |= (1<<Pin.pin);
				} break; // case OUTPUT
			} // switch(mode)
		} break; // case _PD
	} // switch(port)
}

/* Toggling OUTPUT
*/
void pin_toggle(pin_t Pin)
{
	switch(Pin.port)
	{
		case _PA:
		{
			
		} break; // case _PA
		case _PB:
		{
			PORTB ^= (1 << Pin.pin);
		} break; // case _PB
		case _PC:
		{
			PORTC ^= (1 << Pin.pin);
		} break; // case _PC
		case _PD:
		{
			PORTD ^= (1 << Pin.pin);
		} break; // case _PD
	} // switch(port)
}

/* Reading INPUTS
 */
my_bool pin_read(pin_t Pin)
{
	switch(Pin.port)
	{
		case _PA:
		{
			return LOW;
		} break; // case _PA
		case _PB:
		{
			if((PINB & (1 << Pin.pin)) == (1 << Pin.pin))
			{
				return HIGH;
			} else
				return LOW;
		} break; // case _PB
		case _PC:
		{
			if((PINC & (1 << Pin.pin)) == (1 << Pin.pin))
			{
				return HIGH;
			} else
			return LOW;
		} break; // case _PC
		case _PD:
		{
			if((PIND & (1 << Pin.pin)) == (1 << Pin.pin))
			{
				return HIGH;
			} else
			return LOW;
		} break; // case _PD
	} // switch(port)
	
	return 0;		// Written to avoid compiler warnings
}